package qx.hyj.petrescue.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.nio.file.Paths;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 获取项目根目录
        String projectRoot = System.getProperty("user.dir");

        // 构建相对于项目根目录的图片路径
        String imagePath = "file:" + projectRoot + File.separator + "images" + File.separator;

        // 映射物理路径到虚拟URL路径
        registry.addResourceHandler("/images/**")
                .addResourceLocations(imagePath);

        // 添加日志输出以便调试
        System.out.println("图片资源映射: /images/** -> " + imagePath);
    }
}